﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CloudShot.Utils
{
  public class CloudShotRenderer : ToolStripProfessionalRenderer
  {
    private const int DropDownArrowSize = 16;

    private static readonly Pen _borderPen = new Pen(Color.FromArgb(152, 152, 152));

    protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
    {
      e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(220, 220, 220)), e.AffectedBounds);
    }

    protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
    {
      var button = e.Item as ToolStripSplitButton;
      if (button == null)
      {
        base.OnRenderSplitButtonBackground(e);
        return;
      }

      RenderToolStripItem(e.Item, e.Graphics);

      e.Graphics.DrawLine(_borderPen, button.SplitterBounds.X, button.SplitterBounds.Y, button.SplitterBounds.X, button.SplitterBounds.Bottom);
      //e.Graphics.DrawRectangle(_borderPen, button.SplitterBounds);

      Rectangle arrowRectangle = button.DropDownButtonBounds;

      if (CanRenderImageNearArrow(button))
      {
        Rectangle imageRectangle = button.DropDownButtonBounds;
        imageRectangle.Width -= DropDownArrowSize;

        arrowRectangle.Width -= imageRectangle.Width;
        arrowRectangle.Location = new Point(arrowRectangle.Location.X + imageRectangle.Width, arrowRectangle.Location.Y);

        e.Graphics.DrawImageUnscaled(e.Item.Image, imageRectangle.X + (imageRectangle.Width - button.Image.Width) / 2,
                                     imageRectangle.Y + (imageRectangle.Height - button.Image.Height) / 2);
      }

      DrawArrow(new ToolStripArrowRenderEventArgs(e.Graphics, button, arrowRectangle, SystemColors.ControlText, ArrowDirection.Down));
    }

    protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e)
    {
      if (e.Item is ToolStripSplitButton && CanRenderImageNearArrow((ToolStripSplitButton)e.Item))
      {
        e.Item.DisplayStyle = ToolStripItemDisplayStyle.Text;
        return;
      }

      base.OnRenderItemImage(e);
    }

    protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
    {
      var button = e.Item as ToolStripButton;
      if (button == null)
      {
        base.OnRenderSplitButtonBackground(e);
        return;
      }

      RenderToolStripItem(e.Item, e.Graphics);
    }

    protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
    {
      base.OnRenderMenuItemBackground(e);

      Brush normalBrush = Brushes.White;
      Brush brush = new SolidBrush(Color.FromArgb(126, 172, 241));
      RenderToolStripItem(e.Item, e.Graphics, normalBrush, brush, brush, false);
    }

    private static bool CanRenderImageNearArrow(ToolStripSplitButton splitButton)
    {
      return splitButton.Image != null && ((splitButton.Image.Width + DropDownArrowSize) <= splitButton.DropDownButtonWidth);
    }

    private static void RenderToolStripItem(ToolStripItem item, Graphics graphics)
    {
      Brush normalBrush = new LinearGradientBrush(Point.Empty, new Point(0, item.Size.Height), Color.FromArgb(252, 252, 252), Color.FromArgb(218, 218, 218));
      Brush hoverBrush = Brushes.White;
      Brush pressedBrush = new SolidBrush(Color.FromArgb(126, 172, 241));

      RenderToolStripItem(item, graphics, normalBrush, hoverBrush, pressedBrush, true);
    }

    private static void RenderToolStripItem(ToolStripItem item, Graphics graphics, Brush normalBrush, Brush hoverBrush, Brush pressedBrush, bool drawBorder)
    {
      Brush brush;

      if (item is ToolStripButton && ((ToolStripButton)item).Checked)
      {
        brush = pressedBrush;
      }
      else if (item.Selected)
      {
        brush = item.Pressed ? pressedBrush : hoverBrush;
      }
      else
      {
        brush = normalBrush;
      }

      int buttonWidth = item.Size.Width;
      int buttonHeight = item.Size.Height;

      graphics.FillRectangle(brush, new Rectangle(Point.Empty, item.Size));

      if (drawBorder)
        graphics.DrawRectangle(_borderPen, new Rectangle(0, 0, buttonWidth - 1, buttonHeight - 1));
    }
  }
}